package com.example.mymultiactivityapp

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    private lateinit var tvResult: TextView
    private val cameraPermissionCode = 100

    private val resultLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data?.getStringExtra("return_data")
            tvResult.text = getString(R.string.returned_data, data ?: "")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tvResult = findViewById(R.id.tv_result)

        // 按钮1：正向传参
        findViewById<Button>(R.id.btn_forward_pass).setOnClickListener {
            val intent = Intent(this, SecondActivity1::class.java).apply {
                putExtra("forward_data", getString(R.string.sample_data))
            }
            startActivity(intent)
        }

        // 按钮2：返回结果
        findViewById<Button>(R.id.btn_return_result).setOnClickListener {
            val intent = Intent(this, SecondActivity2::class.java)
            resultLauncher.launch(intent)
        }

        // 按钮3：相机拍照
        findViewById<Button>(R.id.btn_camera).setOnClickListener {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
                // 已经有权限，启动相机Activity
                val intent = Intent(this, SecondActivity3::class.java)
                startActivity(intent)
            } else {
                // 请求相机权限
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.CAMERA),
                    cameraPermissionCode
                )
            }
        }

        // 按钮4：秒表功能
        findViewById<Button>(R.id.btn_stopwatch).setOnClickListener {
            val intent = Intent(this, SecondActivity4::class.java)
            startActivity(intent)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == cameraPermissionCode) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，启动相机Activity
                val intent = Intent(this, SecondActivity3::class.java)
                startActivity(intent)
            } else {
                // 权限被拒绝，显示提示
                tvResult.text = "相机权限被拒绝，无法使用拍照功能"
            }
        }
    }
}